#include "CharSelect.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "MainMenuState.h"
#include "GameState.h"
#include "../Game.h"
#include "GameMode.h"
#include "GameMapSelect.h"

CCharSelect* CCharSelect::GetInstance(void)
{
	static CCharSelect s_Instance;
	return &s_Instance;
}

void CCharSelect::Enter(void)
{
	ballp1 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ballp1.png");
	ballp2 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ballp2.png");
	ballp3 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ballp3.png");
	ballp4 = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/ballp4.png");
	characters = SGD::GraphicsManager::GetInstance()->LoadTexture("pictures/characters.png");
}

void CCharSelect::Exit(void)
{
	m_bReady1 = false;
	m_bReady2 = false;
	m_bReady3 = false;
	m_bReady4 = false;

	SGD::GraphicsManager::GetInstance()->UnloadTexture(ballp1);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ballp2);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ballp3);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(ballp4);
	SGD::GraphicsManager::GetInstance()->UnloadTexture(characters);

}

bool CCharSelect::Input(void)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();
	SGD::Vector LeftAnalog1 = pInput->GetLeftJoystick(0);
	SGD::Vector LeftAnalog2 = pInput->GetLeftJoystick(1);
	SGD::Vector LeftAnalog3 = pInput->GetLeftJoystick(2);
	SGD::Vector LeftAnalog4 = pInput->GetLeftJoystick(3);


	if (pInput->IsKeyPressed(SGD::Key::Escape) == true || pInput->IsButtonPressed(0, 6) == true)
	{
		Game::GetInstance()->ChangeState(CGameMode::GetInstance());
		return true;
	}

	if (m_nBlueTeam == 2 && m_nRedTeam == 2 && m_bReady1 == true
		&& m_bReady2 == true
		&& m_bReady3 == true
		&& m_bReady4 == true)
	{
		CGameState::GetInstance()->CreatePlayer({ 40, 40 }, { 32, 51 }, m_uPlayer1, m_bPlayer1Team, 0, SGD::Rectangle{ 0, 50, 300, 100 });
		CGameState::GetInstance()->CreatePlayer({ 40, 40 }, { 32, 51 }, m_uPlayer2, m_bPlayer2Team, 1, SGD::Rectangle{ 500, 50, 800, 100 });
		CGameState::GetInstance()->CreatePlayer({ 40, 40 }, { 32, 51 }, m_uPlayer3, m_bPlayer3Team, 2, SGD::Rectangle{ 0, 550, 300, 600 });
		CGameState::GetInstance()->CreatePlayer({ 40, 40 }, { 32, 51 }, m_uPlayer4, m_bPlayer4Team, 3, SGD::Rectangle{ 500, 550, 800, 600 });
		Game::GetInstance()->ChangeState(CGameMapSelect::GetInstance());
		return true;
	}

	if (pInput->IsButtonPressed(0, 7) == true)
		m_bReady1 = !m_bReady1;

	if (pInput->IsButtonPressed(1, 7) == true)
		m_bReady2 = !m_bReady2;

	if (pInput->IsButtonPressed(2, 7) == true)
		m_bReady3 = !m_bReady3;

	if (pInput->IsButtonPressed(3, 7) == true)
		m_bReady4 = !m_bReady4;

	float LX = LeftAnalog1.x;
	float LY = LeftAnalog1.y;

	float x1 = LX;
	float y1 = LY;

	float dx = 4 * x1;
	float dy = 4 * y1;

	SGD::Vector v = { dx, dy };
	v *= 64;
	m_vtVelocity1 = v;

	LX = LeftAnalog2.x;
	LY = LeftAnalog2.y;

	x1 = LX;
	y1 = LY;

	dx = 4 * x1;
	dy = 4 * y1;

	v = { dx, dy };
	v *= 64;
	m_vtVelocity2 = v;

	LX = LeftAnalog3.x;
	LY = LeftAnalog3.y;

	x1 = LX;
	y1 = LY;

	dx = 4 * x1;
	dy = 4 * y1;

	v = { dx, dy };
	v *= 64;
	m_vtVelocity3 = v;

	LX = LeftAnalog4.x;
	LY = LeftAnalog4.y;

	x1 = LX;
	y1 = LY;

	dx = 4 * x1;
	dy = 4 * y1;

	v = { dx, dy };
	v *= 64;
	m_vtVelocity4 = v;


	return true;
}

void CCharSelect::Update(float elapsedTime)
{
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	m_ptPosition1 += m_vtVelocity1 * elapsedTime;
	m_ptPosition2 += m_vtVelocity2 * elapsedTime;
	m_ptPosition3 += m_vtVelocity3 * elapsedTime;
	m_ptPosition4 += m_vtVelocity4 * elapsedTime;

	KeepInWorld();
	if (m_bReady1 != true)
	{
		if (Runner.IsIntersecting(SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
		{
			// player 1 selected runner 
			if (IsOnTeam(1, m_bPlayer1Team, 1))
				m_uPlayer1 = 1;

		}

		if (Bomber.IsIntersecting(SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
		{
			// player 1 selected bomber 
			if (IsOnTeam(1, m_bPlayer1Team, 2))
				m_uPlayer1 = 2;


		}

		if (Striker.IsIntersecting(SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
		{
			// player 1 selected striker 
			if (IsOnTeam(1, m_bPlayer1Team, 3))
				m_uPlayer1 = 3;


		}

		if (Tank.IsIntersecting(SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true)
		{
			// player 1 selected tank		
			if (IsOnTeam(1, m_bPlayer1Team, 4))
				m_uPlayer1 = 4;

		}

		if (Player1Red.IsIntersecting(SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true || pInput->IsButtonPressed(0, 1) == true)
		{
			if (m_bPlayer1Team)
			{
			}
			else
			{
				if (IsOnTeam(1, !m_bPlayer1Team, m_uPlayer1))
				{
					m_nRedTeam++;
					m_nBlueTeam--;
					m_bPlayer1Team = true;
				}

			}

		}

		if (Player1Blue.IsIntersecting(SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(0, 0) == true || pInput->IsButtonPressed(0, 2) == true)
		{
			if (m_bPlayer1Team)
			{
				if (IsOnTeam(1, !m_bPlayer1Team, m_uPlayer1))
				{
					m_nRedTeam--;
					m_nBlueTeam++;
					m_bPlayer1Team = false;
				}
			}
		}

	}

	if (m_bReady2 != true)
	{
		if (Runner.IsIntersecting(SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(1, 0) == true)
		{
			// player 2 selected runner 
			if (IsOnTeam(2, m_bPlayer2Team, 1))
				m_uPlayer2 = 1;
		}

		if (Bomber.IsIntersecting(SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(1, 0) == true)
		{
			// player 2 selected bomber 
			if (IsOnTeam(2, m_bPlayer2Team, 2))
				m_uPlayer2 = 2;
		}

		if (Striker.IsIntersecting(SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(1, 0) == true)
		{
			// player 2 selected striker 
			if (IsOnTeam(2, m_bPlayer2Team, 3))
				m_uPlayer2 = 3;
		}

		if (Tank.IsIntersecting(SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(1, 0) == true)
		{
			// player 2 selected tank
			if (IsOnTeam(2, m_bPlayer2Team, 4))
				m_uPlayer2 = 4;
		}

		if (Player2Red.IsIntersecting(SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(1, 0) == true || pInput->IsButtonPressed(1, 1) == true)
		{
			if (m_bPlayer2Team)
			{
			}
			else
			{
				if (IsOnTeam(2, !m_bPlayer2Team, m_uPlayer2))
				{
					m_nRedTeam++;
					m_nBlueTeam--;
					m_bPlayer2Team = true;
				}
			}
		}

		if (Player2Blue.IsIntersecting(SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(1, 0) == true || pInput->IsButtonPressed(1, 2) == true)
		{
			if (m_bPlayer2Team)
			{
				if (IsOnTeam(2, !m_bPlayer2Team, m_uPlayer2))
				{
					m_nRedTeam--;
					m_nBlueTeam++;
					m_bPlayer2Team = false;
				}
			}

		}
	}

	if (m_bReady3 != true)
	{
		if (Runner.IsIntersecting(SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(2, 0) == true)
		{
			// player 3 selected runner 
			if (IsOnTeam(3, m_bPlayer3Team, 1))
				m_uPlayer3 = 1;
		}

		if (Bomber.IsIntersecting(SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(2, 0) == true)
		{
			// player 3 selected bomber 
			if (IsOnTeam(3, m_bPlayer3Team, 2))
				m_uPlayer3 = 2;
		}

		if (Striker.IsIntersecting(SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(2, 0) == true)
		{
			// player 3 selected striker 
			if (IsOnTeam(3, m_bPlayer3Team, 3))
				m_uPlayer3 = 3;
		}

		if (Tank.IsIntersecting(SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(2, 0) == true)
		{
			// player 3 selected tank
			if (IsOnTeam(3, m_bPlayer3Team, 4))
				m_uPlayer3 = 4;
		}

		if (Player3Red.IsIntersecting(SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(2, 0) == true || pInput->IsButtonPressed(2, 1) == true)
		{
			if (m_bPlayer3Team)
			{
			}
			else
			{
				if (IsOnTeam(3, !m_bPlayer3Team, m_uPlayer3))
				{
					m_nRedTeam++;
					m_nBlueTeam--;
					m_bPlayer3Team = true;
				}
			}
		}

		if (Player3Blue.IsIntersecting(SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(2, 0) == true || pInput->IsButtonPressed(2, 2) == true)
		{
			if (m_bPlayer3Team)
			{
				if (IsOnTeam(3, !m_bPlayer3Team, m_uPlayer3))
				{
					m_nRedTeam--;
					m_nBlueTeam++;
					m_bPlayer3Team = false;
				}
			}

		}
	}
	if (m_bReady4 != true)
	{
		if (Runner.IsIntersecting(SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(3, 0) == true)
		{
			// player 4 selected runner 
			if (IsOnTeam(4, m_bPlayer4Team, 1))
				m_uPlayer4 = 1;
		}

		if (Bomber.IsIntersecting(SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(3, 0) == true)
		{
			// player 4 selected bomber 
			if (IsOnTeam(4, m_bPlayer4Team, 2))
				m_uPlayer4 = 2;
		}

		if (Striker.IsIntersecting(SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(3, 0) == true)
		{
			// player 4 selected striker 
			if (IsOnTeam(4, m_bPlayer4Team, 3))
				m_uPlayer4 = 3;
		}

		if (Tank.IsIntersecting(SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(3, 0) == true)
		{
			// player 4 selected tank
			if (IsOnTeam(4, m_bPlayer4Team, 4))
				m_uPlayer4 = 4;
		}

		if (Player4Red.IsIntersecting(SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(3, 0) == true || pInput->IsButtonPressed(3, 1) == true)
		{
			if (m_bPlayer4Team)
			{
			}
			else
			{
				if (IsOnTeam(4, !m_bPlayer4Team, m_uPlayer4))
				{
					m_nRedTeam++;
					m_nBlueTeam--;
					m_bPlayer4Team = true;
				}
			}
		}

		if (Player4Blue.IsIntersecting(SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }) && pInput->IsButtonPressed(3, 0) == true || pInput->IsButtonPressed(3, 2) == true)
		{
			if (m_bPlayer4Team)
			{
				if (IsOnTeam(4, !m_bPlayer4Team, m_uPlayer4))
				{
					m_nRedTeam--;
					m_nBlueTeam++;
					m_bPlayer4Team = false;
				}
			}
		}
	}
}

void CCharSelect::Render(void)
{
	const Font* pFont = Game::GetInstance()->GetFont();
	SGD::InputManager* pInput = SGD::InputManager::GetInstance();

	Game* pGame = Game::GetInstance();

	float darkOffset = pGame->DarkOff();
	float medOffset = pGame->MedOff();
	float lightOffset = pGame->LightOff();

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetDarkSmoke(), { darkOffset - 805, 0 }, 0.0f, { 0, 0 }, { 110, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetMedSmoke(), { medOffset - 805, 0 }, 0.0f, { 0, 0 }, { 127, 255, 255, 255 });
	SGD::GraphicsManager::GetInstance()->DrawTexture(pGame->GetLightSmoke(), { lightOffset - 805, 0 }, 0.0f, { 0, 0 }, { 135, 255, 255, 255 });

	pFont->Draw(" Press Start When Ready", { 1, 1 }, 0.6f, { 255, 255, 255 });

	pFont->Draw("Runner", { 20, 250 }, 0.5f, { 255, 255, 255 });
	pFont->Draw("Bomber", { 220, 250 }, 0.5f, { 255, 255, 255 });
	pFont->Draw("Striker", { 420, 250 }, 0.5f, { 255, 255, 255 });
	pFont->Draw("Tank", { 640, 250 }, 0.5f, { 255, 255, 255 });

	SGD::GraphicsManager::GetInstance()->DrawRectangle(Runner, { 0, 0, 0, 0 }, { 255, 255, 0 }, 3U);
	SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 60, 200 }, SGD::Rectangle{ 407, 474, 438, 420 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });

	SGD::GraphicsManager::GetInstance()->DrawRectangle(Bomber, { 0, 0, 0, 0 }, { 0, 255, 0 }, 3U);
	SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 140 }, SGD::Rectangle{ 400, 160, 430, 206 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });

	SGD::GraphicsManager::GetInstance()->DrawRectangle(Striker, { 0, 0, 0, 0 }, { 0, 255, 255 }, 3U);
	SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 140 }, SGD::Rectangle{ 20, 238, 56, 286 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });

	SGD::GraphicsManager::GetInstance()->DrawRectangle(Tank, { 0, 0, 0, 0 }, { 255, 0, 255 }, 3U);
	SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 140 }, SGD::Rectangle{ 18, 490, 60, 542 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });

	pFont->Draw("P1", { 60, 350 }, 0.5f, { 255, 255, 255 });
	pFont->Draw("P2", { 260, 350 }, 0.5f, { 255, 255, 255 });
	pFont->Draw("P3", { 460, 350 }, 0.5f, { 255, 255, 255 });
	pFont->Draw("P4", { 660, 350 }, 0.5f, { 255, 255, 255 });

	//player 1
	if (m_bPlayer1Team)
		SGD::GraphicsManager::GetInstance()->DrawRectangle(SGD::Rectangle{ 30, 400, 150, 550 }, { 0, 0, 0, 0 }, { 255, 0, 0 }, 3U);
	else
		SGD::GraphicsManager::GetInstance()->DrawRectangle(SGD::Rectangle{ 30, 400, 150, 550 }, { 0, 0, 0, 0 }, { 0, 0, 255 }, 3U);


	if (m_uPlayer1 == 1)
	{
		//runner
		if (m_bPlayer1Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 60, 440 }, SGD::Rectangle{ 22, 433, 60, 484 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 65, 440 }, SGD::Rectangle{ 25, 373, 56, 420 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer1 == 2)
	{
		//bomber
		if (m_bPlayer1Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 60, 440 }, SGD::Rectangle{ 400, 160, 430, 206 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 65, 440 }, SGD::Rectangle{ 25, 108, 56, 156 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer1 == 3)
	{
		//striker
		if (m_bPlayer1Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 60, 440 }, SGD::Rectangle{ 20, 238, 56, 286 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 65, 440 }, SGD::Rectangle{ 20, 305, 56, 356 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer1 == 4)
	{
		//tank
		if (m_bPlayer1Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 60, 440 }, SGD::Rectangle{ 18, 490, 60, 542 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 65, 440 }, SGD::Rectangle{ 18, 556, 60, 605 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}


	SGD::GraphicsManager::GetInstance()->DrawRectangle(Player1Red, { 0, 0, 0, 0 }, { 255, 0, 0 }, 3U);
	SGD::GraphicsManager::GetInstance()->DrawRectangle(Player1Blue, { 0, 0, 0, 0 }, { 0, 0, 255 }, 3U);

	//player 2
	if (m_bPlayer2Team)
		SGD::GraphicsManager::GetInstance()->DrawRectangle(SGD::Rectangle{ 230, 400, 350, 550 }, { 0, 0, 0, 0 }, { 255, 0, 0 }, 3U);
	else
		SGD::GraphicsManager::GetInstance()->DrawRectangle(SGD::Rectangle{ 230, 400, 350, 550 }, { 0, 0, 0, 0 }, { 0, 0, 255 }, 3U);

	SGD::GraphicsManager::GetInstance()->DrawRectangle(Player2Red, { 0, 0, 0, 0 }, { 255, 0, 0 }, 3U);
	SGD::GraphicsManager::GetInstance()->DrawRectangle(Player2Blue, { 0, 0, 0, 0 }, { 0, 0, 255 }, 3U);

	if (m_uPlayer2 == 1)
	{
		//runner
		if (m_bPlayer2Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 440 }, SGD::Rectangle{ 22, 433, 60, 484 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 440 }, SGD::Rectangle{ 25, 373, 56, 420 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer2 == 2)
	{
		//bomber
		if (m_bPlayer2Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 440 }, SGD::Rectangle{ 400, 160, 430, 206 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 440 }, SGD::Rectangle{ 25, 108, 56, 156 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer2 == 3)
	{
		//striker
		if (m_bPlayer2Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 440 }, SGD::Rectangle{ 20, 238, 56, 286 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 440 }, SGD::Rectangle{ 20, 305, 56, 356 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer2 == 4)
	{
		//tank
		if (m_bPlayer2Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 440 }, SGD::Rectangle{ 18, 490, 60, 542 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 260, 440 }, SGD::Rectangle{ 18, 556, 60, 605 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}

	//player 3
	if (m_bPlayer3Team)
		SGD::GraphicsManager::GetInstance()->DrawRectangle(SGD::Rectangle{ 430, 400, 550, 550 }, { 0, 0, 0, 0 }, { 255, 0, 0 }, 3U);
	else
		SGD::GraphicsManager::GetInstance()->DrawRectangle(SGD::Rectangle{ 430, 400, 550, 550 }, { 0, 0, 0, 0 }, { 0, 0, 255 }, 3U);

	SGD::GraphicsManager::GetInstance()->DrawRectangle(Player3Red, { 0, 0, 0, 0 }, { 255, 0, 0 }, 3U);
	SGD::GraphicsManager::GetInstance()->DrawRectangle(Player3Blue, { 0, 0, 0, 0 }, { 0, 0, 255 }, 3U);

	if (m_uPlayer3 == 1)
	{
		//runner
		if (m_bPlayer3Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 440 }, SGD::Rectangle{ 22, 433, 60, 484 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 440 }, SGD::Rectangle{ 25, 373, 56, 420 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer3 == 2)
	{
		//bomber
		if (m_bPlayer3Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 440 }, SGD::Rectangle{ 400, 160, 430, 206 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 440 }, SGD::Rectangle{ 25, 108, 56, 156 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer3 == 3)
	{
		//striker
		if (m_bPlayer3Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 440 }, SGD::Rectangle{ 20, 238, 56, 286 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 440 }, SGD::Rectangle{ 20, 305, 56, 356 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer3 == 4)
	{
		//tank
		if (m_bPlayer3Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 440 }, SGD::Rectangle{ 18, 490, 60, 542 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 460, 440 }, SGD::Rectangle{ 18, 556, 60, 605 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}

	//player 4
	if (m_bPlayer4Team)
		SGD::GraphicsManager::GetInstance()->DrawRectangle(SGD::Rectangle{ 630, 400, 750, 550 }, { 0, 0, 0, 0 }, { 255, 0, 0 }, 3U);
	else
		SGD::GraphicsManager::GetInstance()->DrawRectangle(SGD::Rectangle{ 630, 400, 750, 550 }, { 0, 0, 0, 0 }, { 0, 0, 255 }, 3U);

	SGD::GraphicsManager::GetInstance()->DrawRectangle(Player4Red, { 0, 0, 0, 0 }, { 255, 0, 0 }, 3U);
	SGD::GraphicsManager::GetInstance()->DrawRectangle(Player4Blue, { 0, 0, 0, 0 }, { 0, 0, 255 }, 3U);

	if (m_uPlayer4 == 1)
	{
		//runner
		if (m_bPlayer4Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 440 }, SGD::Rectangle{ 22, 433, 60, 484 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 440 }, SGD::Rectangle{ 25, 373, 56, 420 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer4 == 2)
	{
		//bomber
		if (m_bPlayer4Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 440 }, SGD::Rectangle{ 400, 160, 430, 206 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 440 }, SGD::Rectangle{ 25, 108, 56, 156 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer4 == 3)
	{
		//striker
		if (m_bPlayer4Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 440 }, SGD::Rectangle{ 20, 238, 56, 286 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 440 }, SGD::Rectangle{ 20, 305, 56, 356 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}
	else if (m_uPlayer4 == 4)
	{
		//tank
		if (m_bPlayer4Team)
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 440 }, SGD::Rectangle{ 18, 490, 60, 542 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
		else
			SGD::GraphicsManager::GetInstance()->DrawTextureSection(characters, { 660, 440 }, SGD::Rectangle{ 18, 556, 60, 605 }, 0, { 0, 0 }, {}, { 1.5f, 1.5f });
	}

	if (m_bReady1)
		pFont->Draw("Ready", { 20, 510 }, 0.5f, {});

	if (m_bReady2)
		pFont->Draw("Ready", { 220, 510 }, 0.5f, {});

	if (m_bReady3)
		pFont->Draw("Ready", { 420, 510 }, 0.5f, {});

	if (m_bReady4)
		pFont->Draw("Ready", { 620, 510 }, 0.5f, {});


	if (pInput->IsControllerConnected(0))
		SGD::GraphicsManager::GetInstance()->DrawTexture(ballp1, { m_ptPosition1.x, m_ptPosition1.y }, 0, { 0, 0 }, {}, { 0.25f, 0.25f });
	else
		m_bReady1 = true;

	if (pInput->IsControllerConnected(1))
		SGD::GraphicsManager::GetInstance()->DrawTexture(ballp2, { m_ptPosition2.x, m_ptPosition2.y }, 0, { 0, 0 }, {}, { 0.25f, 0.25f });
	else
		m_bReady2 = true;

	if (pInput->IsControllerConnected(2))
		SGD::GraphicsManager::GetInstance()->DrawTexture(ballp3, { m_ptPosition3.x, m_ptPosition3.y }, 0, { 0, 0 }, {}, { 0.25f, 0.25f });
	else
		m_bReady3 = true;

	if (pInput->IsControllerConnected(3))
		SGD::GraphicsManager::GetInstance()->DrawTexture(ballp4, { m_ptPosition4.x, m_ptPosition4.y }, 0, { 0, 0 }, {}, { 0.25f, 0.25f });
	else
		m_bReady4 = true;
}

bool CCharSelect::IsOnTeam(int player, bool team, int character)
{
	if (player == 1)
	{
		if (team == m_bPlayer2Team && m_uPlayer2 == character)
		{
			return false;
		}
		else if (team == m_bPlayer3Team && m_uPlayer3 == character)
		{
			return false;
		}
		else if (team == m_bPlayer4Team && m_uPlayer4 == character)
		{
			return false;
		}
		else
			return true;
	}
	else if (player == 2)
	{
		if (team == m_bPlayer1Team && m_uPlayer1 == character)
		{
			return false;
		}
		else if (team == m_bPlayer3Team && m_uPlayer3 == character)
		{
			return false;
		}
		else if (team == m_bPlayer4Team && m_uPlayer4 == character)
		{
			return false;
		}
		else
			return true;
	}
	else if (player == 3)
	{
		if (team == m_bPlayer1Team && m_uPlayer1 == character)
		{
			return false;
		}
		else if (team == m_bPlayer2Team && m_uPlayer2 == character)
		{
			return false;
		}
		else if (team == m_bPlayer4Team && m_uPlayer4 == character)
		{
			return false;
		}
		else
			return true;
	}
	else if (player == 4)
	{
		if (team == m_bPlayer1Team && m_uPlayer1 == character)
		{
			return false;
		}
		else if (team == m_bPlayer2Team && m_uPlayer2 == character)
		{
			return false;
		}
		else if (team == m_bPlayer3Team && m_uPlayer3 == character)
		{
			return false;
		}
		else
			return true;
	}

	return false;
}

void CCharSelect::KeepInWorld()
{
	//dont go too far left
	if (SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }.left <= 0)
		m_ptPosition1.x = 1;

	//dont go too far up
	if (SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }.top <= 0)
		m_ptPosition1.y = 1;

	//dont go too far right
	if (SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }.left + SGD::Size{ 64, 64 }.width > Game::GetInstance()->GetWorldWidth())
		m_ptPosition1.x = Game::GetInstance()->GetWorldWidth() - SGD::Size{ 64, 64 }.width;

	//dont go too far down
	if (SGD::Rectangle{ m_ptPosition1, SGD::Size{ 64, 64 } }.bottom > Game::GetInstance()->GetWorldHeight())
		m_ptPosition1.y = Game::GetInstance()->GetWorldHeight() - SGD::Size{ 64, 64 }.height;

	//dont go too far left
	if (SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }.left <= 0)
		m_ptPosition2.x = 1;

	//dont go too far up
	if (SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }.top <= 0)
		m_ptPosition2.y = 1;

	//dont go too far right
	if (SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }.left + SGD::Size{ 64, 64 }.width > Game::GetInstance()->GetWorldWidth())
		m_ptPosition2.x = Game::GetInstance()->GetWorldWidth() - SGD::Size{ 64, 64 }.width;

	//dont go too far down
	if (SGD::Rectangle{ m_ptPosition2, SGD::Size{ 64, 64 } }.bottom > Game::GetInstance()->GetWorldHeight())
		m_ptPosition2.y = Game::GetInstance()->GetWorldHeight() - SGD::Size{ 64, 64 }.height;

	//dont go too far left
	if (SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }.left <= 0)
		m_ptPosition3.x = 1;

	//dont go too far up
	if (SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }.top <= 0)
		m_ptPosition3.y = 1;

	//dont go too far right
	if (SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }.left + SGD::Size{ 64, 64 }.width > Game::GetInstance()->GetWorldWidth())
		m_ptPosition3.x = Game::GetInstance()->GetWorldWidth() - SGD::Size{ 64, 64 }.width;

	//dont go too far down
	if (SGD::Rectangle{ m_ptPosition3, SGD::Size{ 64, 64 } }.bottom > Game::GetInstance()->GetWorldHeight())
		m_ptPosition3.y = Game::GetInstance()->GetWorldHeight() - SGD::Size{ 64, 64 }.height;

	//dont go too far left
	if (SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }.left <= 0)
		m_ptPosition4.x = 1;

	//dont go too far up
	if (SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }.top <= 0)
		m_ptPosition4.y = 1;

	//dont go too far right
	if (SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }.left + SGD::Size{ 64, 64 }.width > Game::GetInstance()->GetWorldWidth())
		m_ptPosition4.x = Game::GetInstance()->GetWorldWidth() - SGD::Size{ 64, 64 }.width;

	//dont go too far down
	if (SGD::Rectangle{ m_ptPosition4, SGD::Size{ 64, 64 } }.bottom > Game::GetInstance()->GetWorldHeight())
		m_ptPosition4.y = Game::GetInstance()->GetWorldHeight() - SGD::Size{ 64, 64 }.height;

}
